<?php

namespace app\admin\controller;


use think\Controller;

class Cate extends Controller
{
    //栏目列表
    public function list()
    {

        $cates = model('Cate')->order('sort', 'asc')->paginate('10');

        $viewcate = [
            'cates' => $cates
        ];
        $this->assign($viewcate);
        return view();
    }

    //栏目添加
    public function add()
    {

        if (request()->isAjax()) {
            $data = [
                'catename' => input('post.catename'),
                'sort' => input('post.sort')
            ];
            $result = model('Cate')->add($data);
            if ($result == 1) {
                $this->success("栏目添加成功", 'admin/cate/list');
            } else {
                $this->error($result);
            }
        }

        return view();
    }

    //排序
    public function sort()
    {
        $data = [
            "id" => input('post.id'),
            "sort" => input('post.sort')
        ];
        $result = model('Cate')->sort($data);
        if ($result == 1) {
            $this->success("排序成功", "admin/cate/list");
        } else {
            $this->error($result);
        }
    }

    //编辑
    public function edit($id)
    {

        if (request()->isAjax()){
                $data = [
                    'id' => $id,
                    'catename' => input('post.catename'),
                    'sort' => input('post.sort')
                ];
                $result = model("Cate")->edit($data);
                if ($result == 1){
                    $this->success("修改成功", 'admin/cate/list');
                }else{
                    $this->error($result);
                }
        }else{
            $cateInfo = model("Cate")->find(['id' => $id]);
            $this->assign('cateInfo',$cateInfo);
        }
        return view();
    }

    //删除
    public function del(){
        $cateInfo = model('Cate')->with('article_list')->find(input('post.id'));
        $result = $cateInfo->together('article_list')->delete();
        if ($result){
            $this->success("删除成功",'admin/cate/list');
        }else{
            $this->error('删除失败');
        }
    }


}
